package com.jae.controller.adminUser;


import com.jae.controller.BaseController;
import com.jae.model.ResultMsg;
import com.jae.model.ServiceRuntimeException;
import com.jae.model.adminUser.AdminUser;
import com.jae.model.user.User;
import com.jae.service.AdminUserService;
import com.jae.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台管理员Controller
 * 
 * @author Jae
 * @email 1571878499@qq.com
 * @date 2020-12-09 12:21:16
 */
@RestController
@RequestMapping("/admin/adminUser")
public class AdminUserAdminController extends BaseController {

	@Autowired
	private AdminUserService adminUserService;

	/**
	 * /admin/AdminUser/all 所有后台管理员列表
	 */
	@RequestMapping(value = "/all", method = RequestMethod.POST)
	public ResultMsg all(HttpServletRequest request, String user_id, String username, String realname,
						 String tel) {
		String interfaceName = "所有后台管理员列表";
		try {
			Map map = new HashMap();
			if (StringUtil.hasTest(user_id))
				map.put("user_id", user_id.trim());
			if (StringUtil.hasTest(username))
				map.put("username", username.trim());
			if (StringUtil.hasTest(realname))
				map.put("realname", realname.trim());
			if (StringUtil.hasTest(tel))
				map.put("tel", tel.trim());
			return new ResultMsg(true, adminUserService.findAll(map));
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

	/**
	 * /admin/adminUser/info 获取指定后台管理员信息
	 */
	@RequestMapping(value = "/info")
	public ResultMsg info(HttpServletRequest request, Integer user_id) {
		String interfaceName = "获取指定后台管理员信息";
		try {
			return new ResultMsg(true, adminUserService.findById(user_id));
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

	/**
	 * /admin/adminUser/add 添加后台管理员
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ResultMsg add(HttpServletRequest request, AdminUser adminUser, String role_ids) {
		String interfaceName = "添加后台管理员";
		try {
			User user = getUser();
			return new ResultMsg(true, adminUserService.insert(adminUser,role_ids, user));
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

	/**
	 * /admin/adminUser/update 修改后台管理员
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResultMsg update(HttpServletRequest request, AdminUser adminUser) {
		String interfaceName = "修改后台管理员";
		try {
			if (adminUserService.update(adminUser, getUser()))
				return new ResultMsg(true);
			return new ResultMsg(false, "修改后台管理员失败");
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

	/**
	 * /admin/adminUser/delete 物理删除后台管理员
	 */
	@RequestMapping(value = "/delete")
	public ResultMsg delete(HttpServletRequest request, Integer user_id) {
		String interfaceName = "物理删除后台管理员";
		try {
			// if(adminUserService.updateDelete(user_id, getUser()))
			if (adminUserService.delete(user_id))
				return new ResultMsg(true);
			return new ResultMsg(false, "删除失败");
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

}
